//
//  RMMNoPasswordViewController.m
//  Romomo
//
//  Created by puman on 2016/10/19.
//  Copyright © 2016年 mazegong. All rights reserved.
//

#import "RMMNoPasswordViewController.h"
#import "RMMLoginView.h"
#import "RMMAginPasswordViewController.h"
#import "RMMProtocolViewController.h"

@interface RMMNoPasswordViewController ()<UITextFieldDelegate>

@end

@implementation RMMNoPasswordViewController
{
    RMMLoginView *_loginView;
    UITextField *_textPhone;
    UITextField *_yanText;
    UIButton *_button;
    UIButton *_sendBut;
    NSInteger _number;
    NSString *_yanCode;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kWhiteColor;
    self.navigationItem.title= @"密码登录";
    _number = 60;
    [self addViews];
    // Do any additional setup after loading the view.
}
- (void)addViews{
    _loginView = [[RMMLoginView alloc] initWithFrame:CGRectMake(0, 0, kScreenWideth, kScreenHeight - 64)];
    [self.view addSubview:_loginView];
    _loginView.userName.delegate = self;
    _loginView.password.delegate = self;
    _loginView.password.secureTextEntry = YES;
    TEXTFIELD_S(_loginView.userName, beginLanding:);
    TEXTFIELD_S(_loginView.password, beginLanding:);
    _loginView.userName.keyboardType = UIKeyboardTypeNumberPad;
    BUTTON_S(_loginView.loginBut, loginButClick);
    BUTTON_S(_loginView.forgetPasswordBut, forgetPasswordButClick);
    BUTTON_S(_loginView.procotolBut, procotolButtClick);

}
- (void)procotolButtClick{
    [self.navigationController pushViewController:[[RMMProtocolViewController alloc] init] animated:YES];
}
- (void)loginButClick{
    if (![RMMFileManager isPhoneNumberWith:_loginView.userName.text]) {
        [self.view endEditing:YES];
        [RMMFileManager MBProgressWith:@"手机号码格式错误!" onView:self.view];
        return;
    }
    [self loginRequestWith:@"1"];
}
- (void)loginRequestWith:(NSString *)loginType{
    [self.view endEditing:YES];
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"org_no"] = Org_no;
    parDic[@"login_type"] = loginType;
    parDic[@"mobile"] = _loginView.userName.text;
    parDic[@"openid"] = @"";
    parDic[@"password"] = _loginView.password.text;
    NSString *url = [NSString stringWithFormat:@"%@",LogingUrl];
    [OSRequestManager requestFWithUrlString:url parDic:parDic method:POST finish:^(NSDictionary *dataDic) {
        [RMMFileManager shareHandle].userDic = [RMMFileManager setValueForDic:dataDic[@"data"]];
        NS_POST(@"LANDSUCESS", nil, nil);
        [RMMFileManager shareHandle].isLoginSuccess = YES;
        [RMMFileManager MBProgressWith:@"登录成功!" onView:self.view];
        [[RMMFileManager shareHandle] saveVale:[NSString stringWithFormat:@"%@",dataDic[@"data"][@"CUST_ID"]] forKey:@"CUST_ID"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.navigationController popToRootViewControllerAnimated:YES];
        });
        
    } error:^(NSError *errors) {
        
    } view:self.view];
}

- (void)beginLanding:(UITextField *)text{
    if (_loginView.userName.text.length != 0 && _loginView.password.text.length != 0) {
        _loginView.loginBut.layer.borderColor = kBasicColor.CGColor;
        [_loginView.loginBut setTitleColor:kBasicColor forState:(UIControlStateNormal)];
        _loginView.loginBut.userInteractionEnabled = YES;
    } else {
        _loginView.loginBut.layer.borderColor = COLORFROMCODE(0xF3F2F3 , 1).CGColor;
        [_loginView.loginBut setTitleColor:COLORFROMCODE(0xD7D7D7, 1) forState:(UIControlStateNormal)];
        _loginView.loginBut.userInteractionEnabled = NO;
    }
    if (text == _loginView.userName) {
        if (_loginView.userName.text.length > 11) {
            _loginView.userName.text = [_loginView.userName.text substringToIndex:11];
        }
    }
}
// 忘记密码
- (void)forgetPasswordButClick{
    RMMAginPasswordViewController *VC = [[RMMAginPasswordViewController alloc] init];
    [self.navigationController pushViewController:VC animated:YES];
}

- (void)yanzheng{
    if (![_yanText.text isEqualToString:_yanCode]) {
        [RMMFileManager MBProgressWith:@"验证码输入错误!" onView:self.view];
        return;
    } else {
        self.block(_textPhone.text);
        [self.navigationController popViewControllerAnimated:YES];
    }
}
- (void)doneBut{
    [self.view endEditing:YES];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
